home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4647 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.7 KB  |  70 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.sprintlink.net!news1!news
  3. From: pbennett@acraline.com (Patrick Bennett)
  4. Subject: Re: Multithreading with BC4.5
  5. X-Nntp-Posting-Host: machine131.acraline.com
  6. Message-ID: <310f8e18.2686784@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Reply-To: pbennett@acraline.com
  9. Organization: Acraline Products
  10. X-Newsreader: Forte Agent .99c/16.141
  11. References: <4ejl6d$1ck@news00.btx.dtag.de>
  12. Date: Wed, 31 Jan 1996 15:48:14 GMT
  13.  
  14. PHoersemann@t-online.de (Peter Hrsemann) wrote:
  15.  
  16. >Hello Folks !
  17. >
  18. >This is my first posting to a newsgroup and I hope to get plenty of
  19. >feedback :-)
  20.  
  21. Good or bad feedback - there's plenty of it here!  Regrettably, more
  22. often than not, there's quite a bit more of the latter.
  23. >
  24. >My problem:
  25. >I want to call a member function of an existing object as a background
  26. >thread. Therefore I created a friend function in its class :
  27. >
  28. >void friendfunc(void* obj)
  29. >
  30. >, which is called by:
  31. >
  32. >_beginthread(friendfunc,4096,(void*)obj);
  33. >
  34. >In this friend function, I want to call a member function of obj.
  35. >Because of its actual type void*, I have to do a type conversion.
  36. >But how ?? What is the correct type conversion, so I am able to call
  37. >obj.memberfunc ?
  38. >Is there a complete different (better?) way to solve the whole problem
  39. >?
  40.  
  41. The function address you provide to _beginthread will need to be to a
  42. 'static' function within a class (if you want it to be within a
  43. class).  You might want to take a look at the C++ FAQ.  I believe it
  44. covers why this is so.  
  45.  
  46. As for threads in Borland, you might want to take a look at the
  47. TThread class that comes with Borland.  It makes using threads pretty
  48. darn easy.  All you have to do is derive a class of yours from TThread
  49. and provide a 'Run' method.  The run method is what gets spawned as a
  50. separate thread when you Start() the thread.  Your run method can then
  51. periodically check the ShouldTerminate() status to determine when/if
  52. it should exit.  For simple uses, I usually put a call to Start() in
  53. the constructor of the class, and a call to Terminate() (which sets
  54. the 'Terminate' flag) in the destructor.
  55.  
  56. One thing though which bit me BIG TIME.  You need to do a compile in
  57. the bc45\source\classlib directory (look at the makefile) of the
  58. classlib's with MULTITHREADING turned on (-DMT).  Otherwise, the
  59. default classlib's use 'CreateThread' instead of _beginthreadNT which
  60. caused me an endless amount of heartache.  My programs worked and the
  61. threads worked, but as soon as I threw an exception in a thread - WHAM
  62. - it was toast.  Because the lib's used CreateThread instead of
  63. _beginthreadNT the thread wasn't setup properly for the RTL.
  64.  
  65.  
  66. -------------------------------------------
  67. Patrick Bennett
  68. Acraline Products, Inc.
  69.  pbennett@acraline.com / CIS: 74103,2115
  70.